#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:chengyanan
@file: first_ddt_case.py
@time: 2019/12/22  5:58 下午
"""
import sys
import os

path = os.path.dirname(os.getcwd())
sys.path.append(path)
from case import HTMLTestRunner
import unittest
from business.register_business import RegisterBusiness
from selenium import webdriver
import ddt
import time
from util.excel_util import ExcelUtile

ex = ExcelUtile()
data = ex.get_data()


@ddt.ddt
class FirstDdtCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://www.jd.com")
        self.login = RegisterBusiness(self.driver)

    def tearDown(self):
        t = time.strftime("%m-%d%H:%M", time.localtime())
        for method_name, error in self._outcome.errors:
            if error:
                case_name = self._testMethodName
                self.driver.save_screenshot("../report/" + t + case_name + ".png")

            self.driver.quit()

    # @ddt.data(
    #     ["", "password", "name_null", "请输入账户名"],
    #     ["username", "", "password_null", "请输入密码"],
    #     ["", "", "name_and_password_null", "请输入账户名和密码"],
    # )
    # @ddt.unpack
    # def test_register_case(self, name, password, assertcode, asserttext):
    #     name_null = self.login.register_function(name, password, assertcode, asserttext)
    #     time.sleep(3)
    #     print(name_null)
    #     self.assertFalse(name_null, '测试失败')

#对应的business的register_business.py的register_function函数

    @ddt.data(*data)
    def test_register_case(self, data):
        name, password, assertcode, asserttext = data
        name_null = self.login.register_function(name, password, assertcode, asserttext)
        time.sleep(3)
        self.assertFalse(name_null, '测试失败')


if __name__ == '__main__':
    file_path = "../report/first_case1.html"
    fp = open(file_path, "wb")
    suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase)
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="this is First report1", description="京东报告1", verbosity=2)
    runner.run(suite)
